package com.learn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.learn.utils.MqttClient;
import com.learn.utils.R;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 *
 */

@RestController
@RequestMapping("data")
public class DataController extends AbstractController implements ApplicationListener<ContextRefreshedEvent> {
    //static静态全局属性
    public static String RFID = null;
    public static String DATA = null;
    public static String I1 = null;
    public static String I3 = null;

    public static String I2 = null;


    @RequestMapping("/rfid")
    public R warn() {

        String s= null;
        if(RFID !=null){
            s = RFID;
            RFID = null;
        }

        return R.ok().put("data", s);
    }


    @RequestMapping("/get")
    public R get() {

        return R.ok().put("data",DATA);
    }
    //初始化客户端
    public static String code = "2039";
    //throws InterruptedException抛出异常
    private static void send(String cmd) throws InterruptedException {
        //该方法调用可以看做在finally块中，所以资源的释放一定会执行，不过能不能成功释放还是得看close方法是否正常返回。
        try {
            //MQTT client模式
            MqttClient.publish(code, cmd + "ed");
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    //把需要处理的操作放在onApplicationEvent中进行处理
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

//        DataService dataService = (DataService) SpringContextUtils.getBean("dataService");
//        Task task = (Task) SpringContextUtils.getBean("testTask");
        //把当前的类实例化为一个线程，启动该线程
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    //Thread.sleep()是Thread类的一个静态方法，使当前线程休眠，进入阻塞状态（暂停执行），
                    // 如果线程在睡眠状态被中断，将会抛出IterruptedException中断异常。
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                MqttClient.subscribe(code, new MqttCallback() {

                    public void connectionLost(Throwable arg0) {

                    }

                    public void messageArrived(String topic, MqttMessage message) throws Exception {
                        System.out.println("MQTT Rece:" + message.toString());

                        JSONObject json = JSON.parseObject(message.toString());
                            //获得RFID唯一编码被返回到前端的json格式
                            RFID = json.getString("rfid");

                        System.out.println("get save ok!");

                    }

                    public void deliveryComplete(IMqttDeliveryToken token) {

                    }

                });
            }
        });
        t.start();



    }


}

